package com;

import com.spire.pdf.FileFormat;
import com.spire.pdf.PdfDocument;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;

/**
 * @author liujun
 * @date 2023年11月15日18:17
 */
public class ReadPDFUtil1 {
    private final static Logger LOGGER = LoggerFactory.getLogger(ReadPDFUtil1.class);
    public static void main(String[] args) throws FileNotFoundException {
        String path = "F:\\works\\back\\2023209509280_专利申请受理通知书_一种智能运输设备_2023042501052000_ZL075012_.pdf";
        String str = readPDF(path);
        System.out.print(str);
    }

    public static String readPDF(String path) {
        try {
            //加载PDF文档
            PdfDocument pdf = new PdfDocument();
            pdf.loadFromFile(path);

            //设置useEmbeddedSvg和 useEmbeddedImg布尔值为true
            pdf.getConvertOptions().setPdfToHtmlOptions(true,true);

            //保存到流
            File outFile = new File("toHTML.html");
            OutputStream outputStream = new FileOutputStream(outFile);

            pdf.saveToStream(outputStream, FileFormat.HTML);

            String str = FileUtils.readFileToString(outFile,"UTF-8");
//            String html = StringUtils.substringBeforeLast(str,"</body>");
//            String html1 = StringUtils.substringAfterLast(str,"</body>");
//           // String html2 = "<script type='text/javascript'>const synth = window.speechSynthesis;const ssu = new SpeechSynthesisUtterance('哈哈哈');synth.speak(ssu);</script>";
//            StringBuilder builder = new StringBuilder();
//            builder.append("<script type='text/javascript'>const synth = window.speechSynthesis;");
//            builder.append("function speakMes(var msg){const ssu = new SpeechSynthesisUtterance(msg);synth.speak(ssu);");
//            builder.append("var tags = document.body.getElementByTagName('*');for(var i=0;i<tags.length;i++){");
//            builder.append("var txt1 = tags[i].innerText; var len = txt1.length;");
//            builder.append("if(len==1){speakMes(txt1);}else{for(var j=0;j<len;j++){");
//            builder.append("for(var j=0;j<len;j++){");
//
//            String html3 = html+html2+html1;
//
//            FileUtils.write(outFile,html3,"UTF-8");

            // outFile.delete();

            return str;
        } catch (Exception e) {
            LOGGER.error("读取[{}]文件失败",path,e);
        }

        return null;
    }

}
